<?php

/**
 * @author 		Christopher Hazekamp
 * @copyright 	2011
 */

class Content extends Database
{
	public $tpl_vars = array();
	
	function __construct()
	{
		global $config;
		$this->tpl_vars['MAIN_PATH'] = $config->get('domain_protocol') . $config->get('site_domain') . $config->get('site_path');
		$this->tpl_vars['SITE_NAME'] = $config->get('site_name');
		$this->tpl_vars['PAGE_STYLE'] = $this->tpl_vars['MAIN_PATH'] . 'output/cfh/style.css';
		
	}
	
	public function get_page($id, $lang='en')
	{
		$get_page =	parent::sql_query("
					SELECT		*
					FROM		" . TABLE_PAGES . "	p
					LEFT JOIN	" . TABLE_PAGES_CONTENT . " c
					ON			(c.pct_page_id = p.page_id)
					WHERE		c.pct_var	= '".$id."'
					AND			c.pct_lang	= '".$lang."'
					LIMIT		1
					");
		$row_page = parent::sql_fetch_assoc($get_page);
		$this->tpl_vars['PAGE_TITLE'] = $row_page['pct_title'];
		return $row_page;
	}
	
	public function get_parts($id, $lang='en')
	{
		global $string;
		global $tpl;
		$get_content = parent::sql_query("
						SELECT		*
						FROM		" . TABLE_PAGES_CONTENT . "
						WHERE		pct_page_id = '" . intval($id) . "'
						ORDER BY	pct_position ASC
						");
		while($row_content = parent::sql_fetch_assoc($get_content))
		{
			switch ($row_content['pct_type'])
			{
				case 'default':
					$page_template	= 'output/cfh/content_body.html';
				break;
				
				case 'module':
					$page_template= 'modules/' . $row_content['pct_module'] . '/output/default/' . $row_content['pct_module_part'] . '_body.html';
				break;
				
				case 'media':
					$media = $this->load_media($row_content['content_media'], $row_content['content_media_type']);
					$page_template	= '';
				break;
			}
			$array_content[] = array(
				'title'		=> $row_content['pct_title'],
				'content' 	=> $string->text($row_content['pct_content']),
				'template'	=> $page_template,
				'type'		=> $row_content['pct_type'],
				'module'	=> $row_content['pct_module'],
				'module_part' => $row_content['pct_module_part']
			);
		}
		return $array_content;
	}
	
	public function get_category($id, $language='en')
	{
		global $database;
		$get_category	= 	"
							SELECT		cat.*, con.*
							FROM		" . TABLE_CATEGORIES . " cat
							INNER JOIN	" . TABLE_CATEGORIES_CONTENT . " con
							ON			(con.catc_group_id = cat.cat_id)
							WHERE		con.catc_group_id = :catc_group_id
							AND			con.catc_language = :catc_language
							LIMIT		0, 1
							";
		$database->query($get_category);
		$database->bind(':catc_group_id', $id);
		$database->bind(':catc_language', $language);
		$row = $database->single();
		return $row['catc_title'];
	}
	
	public function get_media($id)
	{
		global $database, $main_path;
		$get_media	=	"
						SELECT		med_id,med_type,med_file
						FROM		" . TABLE_MEDIA . "
						WHERE		med_id = :med_id
						LIMIT		0, 1
						";
		$database->query($get_media);
		$database->bind(':med_id', $id);
		$row = $database->single();
		/*switch ($row['med_type'])
		{
			case MEDIA_TYPE_IMAGE:
				
				break;
				
			case MEDIA_TYPE_AUDIO:
			
				break;
				
			case MEDIA_TYPE_VIDEO:
			
				break;
				
			default:
			
				break;
		}*/
		$mediafile = $main_path . MEDIA_PATH_CONTENT . $row['med_file'];
		return $mediafile;
	}
	
	public function load_module($module, $part)
	{
		$module_to_load = PATH_MODULES . $module . '/' . $part . EXT_PHP;
		if (file_exists($module_to_load) && is_readable($module_to_load))
		{
			include($module_to_load);
		}
		else
		{
			throw new ErrorHandler ('The module could not be found');
		}
	}
	
	public function load_media($media, $type)
	{
		switch ($type)
		{
			case 'audio':
				
			break;
			
			case 'image':
			
			break;
			
			case 'video':
			
			break;
		}
	}
	
	
}

?>